<?php
// +-------------------------------------------------------------
// | Name： 星空框架 用户管理模型
// +-------------------------------------------------------------
// | Version: V5.0
// +-------------------------------------------------------------
// | Author: xkyinzhe <zhonghuaxinxing@sina.cn>
// +-------------------------------------------------------------
// | Time: 2021-05-07 11:13
// +-------------------------------------------------------------
declare(strict_types=1);

namespace app\common\model;

use think\facade\Cache;

/**
 * @xk 用户管理模型
 */
class Member extends BaseModel
{
    protected $name = 'member';
    protected $pk = 'id';
    /**
     * @xk 写入前操作
     *
     * @method onBeforeWrite
     * @param  [type]                  $data                     [description]
     * @return [type]                                            [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-05-07T11:14:40+080
     */
    public static function onBeforeWrite($data)
    {
        unset($data['act']);
        unset($data['controllers']);
        unset($data['actions']);
        if (!$data->create_time) {
            $data->update_time = $data->create_time = date('Y-m-d H:i:s');
        }
        $pass = isset($data['pass'])?$data['pass']:'';
        $next_pass = isset($data['next_pass'])?$data['next_pass']:'';
        unset($data['next_pass']);
        if ($pass != '' && $pass == $next_pass) {
            $data->pass_key = self::getCodeNo('zxr', 8, 5);
            $data->pass = makePass($pass, $data->pass_key);
        }
    }
    public static function getUserCodeNo(){
      //  获取会员编号
      $start = 1012333;
      $count = Self::count();
      if($count <= 0){
        return $start;
      }else{
        return self::max('user_sn')+1;
      }
    }
    /**
     * @xk 检测手机号
     *
     * @method checkMobile
     * @param  [type]                  $tel                      [description]
     * @param  [type]                  $act                      [description]
     * @return [type]                                            [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-05-07T13:46:20+080
     */
    public static function checkMobile($tel, $act)
    {
        $condition = [];
        $condition[] = ['tel', '=', $tel];
        switch ($act) {
          case 'register':
            $count = self::where($condition)->count();
            if ($count > 0) {
                return false;
            }

            return true;
          case 'bind':
              $condition[] = ['is_bind','=',1];
              $count = self::where($condition)->count();
              if ($count > 0) {
                  return false;
              }

            return true;
            break;
            case 'login':
              $count = self::where($condition)->count();
              if ($count <= 0) {
                  return false;
              }
              return true;
              break;
          case 'forget':
            $count = self::where($condition)->count();
            if ($count <= 0) {
                return false;
            }
            return true;
            break;
        }
        return false;
    }
    /**
     * @xk 检测验证码
     *
     * @method checkCaptch
     * @param  [type]                  $tel                      [description]
     * @param  [type]                  $act                      [description]
     * @param  [type]                  $captch                   [description]
     * @return [type]                                            [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-05-07T13:47:52+080
     */
    public static function checkCaptch($tel, $act, $hasCaptch)
    {
        $captch = $tel ? Cache::get('sms_'.$act.'_'.$tel, '') : '';
        if (!$captch) {
            return false;
        }
        if ($captch != $hasCaptch) {
            return false;
        }
        Cache::delete('sms_'.$act.'_'.$tel);

        return true;
    }
    /**
     * @xk 生成会员二维码
     *
     * @method getUserEwm
     * @param  [type]     $user_sn                   [description]
     * @return [type]     [description]
     * @Time   2021-10-20
     * @City   青岛
     * @Author xkyinzhe   <zhonghuaxinxing@sina.cn>
     */
    public static function getUserEwm($user_sn){
      if(!$user_sn){
        return  '';
      }
      require_once(__DIR__ . '/../../../extend/phpQrcode/phpqrcode.php');
      $path = 'upload/ewm'; // 用户生成的会员码 每个用户一个
      $share_url = 'https://'.$_SERVER['HTTP_HOST'].'/#/pages/index/index?share_code='.base64_encode($user_sn);
      // echo $share_url; die();
      $qrcode = new \QRcode();
      $value = $share_url;                    //二维码内容
      $errorCorrectionLevel = 'H';    //容错级别
      $matrixPointSize = 2;           //生成图片大小
      ob_start();
      if(!file_exists($path)){
        mkdir($path, 0777, true);
      }
      $qrcodename = $path.'/'.$user_sn.'.png';
      $qrcode::png($value,$qrcodename , $errorCorrectionLevel, $matrixPointSize, 2);
      imagecreatefromstring(file_get_contents($qrcodename));
      return $qrcodename;
    }
}
